home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Tutorial / Script5a.vu < prev    next >
Encoding:
Text File  |  1998-06-04  |  7.2 KB  |  254 lines  |  [TEXT/MPS ]

  1. # **********************************************************************
  2. # File:                Script 5 - TargetA.vu
  3. #
  4. # Purpose:            to demonstrate the ability to use message passing
  5. #
  6. # Prerequisites:    This script assumes that DrawShapesVU is the
  7. #                    currently active application on the target 
  8. #                    computer.
  9. #
  10. # Written by:        David Gaxiola
  11. #
  12. # Copyright © 1992 by Apple Computer, Inc., all rights reserved.
  13. #
  14. # **********************************************************************
  15.  
  16. Libraries "StandardDialogs.vulib", "Time.vulib";
  17.  
  18. # **********************************************************************
  19. # Calculate the absolute difference between two numbers.
  20. task Diff( Num1, Num2 ) 
  21. begin
  22.     theDifference := Num1 - Num2;
  23.     if (theDifference < 0) 
  24.     begin
  25.         theDifference := -theDifference;
  26.     end;
  27.     return theDifference;
  28. end;
  29.  
  30. # **********************************************************************
  31. # Wait for the the partner to give the "go."
  32. # Using the wait() task lessens the burden on Virtual User.
  33. task HoldForOther( )
  34. begin
  35.     global gPartner;
  36.     
  37.     status := {''};
  38.     println "Holding for {gPartner} at ", GetCurrentTime( false ), "…"; 
  39.     while not (status = 'go') 
  40.     begin
  41.         status := Receive([actor t:gPartner]);
  42.         if not ( status = 'go' ) 
  43.             wait( 2 );
  44.     end;
  45.     print "Proceeding at ", GetCurrentTime( false ), "…";
  46. end;
  47.  
  48. # **********************************************************************
  49. # Select a given or random tool from the side palette.
  50. task SelectTool(toolNum := 1, randomDraw := false) 
  51. begin
  52.     global gWindowDim;
  53.     global gToolColumn;
  54.     global gToolRow;
  55.     
  56.     if ( randomDraw ) 
  57.         toolNum := Random(2,4);
  58.     move a:{ (gWindowDim[1] + gToolColumn), 
  59.                 (gWindowDim[2] + gToolRow[toolNum]) };
  60.     click;
  61. end;
  62.  
  63. # **********************************************************************
  64. # Select a random color from the Color Picker dialog box.
  65. task SelectFromColorPicker( ) 
  66. begin
  67.     global gPickerMaxScrollValue;
  68.     global gColorPickerFixedCenter;
  69.     global gColorPickerRadius;
  70.  
  71.     match [ window o:1 r:?colorPickerDim ];
  72.     scrollValue := Random( 1, gPickerMaxScrollValue );
  73.     scroll[ scrollbar w:[ window o:1 ]] a:{ scrollValue, 
  74.     gPickerMaxScrollValue };
  75.     wait( 1 );
  76.     colorPickerCenter := { (gColorPickerFixedCenter[1] +
  77.                             colorPickerDim[1]), 
  78.                             (gColorPickerFixedCenter[2] + 
  79.                             colorPickerDim[2]) };
  80.     xPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  81.     yPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  82.  
  83.     while ( (xPos * xPos) + (yPos * yPos) > gColorPickerRadius *
  84.             gColorPickerRadius ) 
  85.     begin
  86.         xPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  87.         yPos := Random(0, (gColorPickerRadius * 2)) - gColorPickerRadius;
  88.     end;
  89.  
  90.     x := gColorPickerCenter[1] + xPos;
  91.     y := gColorPickerCenter[2] + yPos;
  92.     move a:{ x, y };                    
  93.     click;    
  94.     select [ button t:'OK' w:[ window o:1 ]]!;        
  95. end;
  96.  
  97. # **********************************************************************
  98. # task to select one of the seven preset colors or the Color Picker
  99. task SelectColor(colorNum := 1, randomColor := false) 
  100. begin
  101.     global gColorAvail;
  102.  
  103.     if (gColorAvail)  
  104.     begin
  105.         if randomColor  
  106.         begin
  107.             colorNum := Random( 1, 9 );
  108.             while ( colorNum = 8 )  
  109.                 colorNum := Random( 1, 9 );
  110.         end;
  111.         select[ menuItem o:colorNum m:[ menu t:'Colors' ]];
  112.         if colorNum = 9  
  113.             SelectFromColorPicker( );
  114.     end;
  115. end;
  116.  
  117. # *********************************************************************
  118. # task to select one of the five possible shades
  119. task SelectShade(shadeNum := 1, randomShade := false) 
  120. begin
  121.     global gShadeMenuPos;
  122.     global gShadeBlockSize;
  123.     global gShadePositions;
  124.  
  125.     shadeLoc := gShadePositions[shadeNum];
  126.     if ( randomShade )  
  127.     begin
  128.         shadeNum := Random( 1, 5 );
  129.         ShadeLoc := gShadePositions[shadeNum];
  130.     end;
  131.     x := gShadeMenuPos[1];
  132.     y := gShadeMenuPos[2];
  133.     move a:{ x, y };
  134.     pressMouse;    
  135.     wait( 1 );
  136.     x := gShadeBlockSize * shadeLoc[1];
  137.     y := gShadeBlockSize * shadeLoc[2];
  138.     move r:{ x, y };
  139.     Wait( 1 );    # for slower targets
  140.     releaseMouse;
  141. end;
  142.  
  143. # **********************************************************************
  144. # task to draw a rectangular shape
  145. task DrawRandomShapeWithTool( ) 
  146. begin
  147.     global gWindowDim;
  148.     global gWindowMod;
  149.  
  150.     range1 := ( gWindowDim[1] + gWindowMod[1] );
  151.     range2 := ( gWindowDim[2] + gWindowMod[2] );
  152.     range3 := ( gWindowDim[3] + gWindowMod[3] );
  153.     range4 := ( gWindowDim[4] + gWindowMod[4] );
  154.     xStart := Random( range1, range3 );
  155.     yStart := Random( range2, range4 );
  156.     xStop := Random( range1, range3 );
  157.     yStop := Random( range2, range4 );
  158.  
  159.     while ( Diff( xStart, xStop ) < 40 )  
  160.     begin
  161.         xStart := Random( range1, range3 );
  162.         xStop := Random( range1, range3 );
  163.     end;
  164.     
  165.     while ( Diff( yStart, yStop ) < 40 )  
  166.     begin
  167.         yStart := Random( range2, range4 );
  168.         yStop := Random( range2, range4 );    
  169.     end;
  170.     
  171.     move a:{ xStart,yStart }; 
  172.     pressMouse;
  173.     wait( 1 ); # for slower targer
  174.     move a:{ xStop,yStop };  
  175.     releaseMouse;
  176. end;
  177.  
  178. # Begin main body. *****************************************************
  179. # Note:  Replace "Virtual User-2" with the name of the second target.
  180. script Tutorial5AMain(gPartner := "Virtual User-2", maxIterations := 2, saveAsFilename := 'Tutorial 5A Example.ds')
  181.  
  182. begin
  183. # global variable definitions 
  184.     global gToolColumn := 18;
  185.     global gToolRow := { 40, 80,120,160 };
  186.     global gWindowMod := { 41, 21, -21, -21 };
  187.     global gPickerMaxScrollValue := 32767;
  188.     global gColorPickerFixedCenter := {293,137};
  189.     global gColorPickerRadius := 100;
  190.     global gShadeMenuPos    := {125,10};
  191.     global gShadeBlockSize := 25;
  192.     global gShadePositions := { {0,1},{1,1},{2,1},{0,2},{1,2},{2,2} };
  193.     global gWindowDim := { };
  194.     global gScreenDim := { };
  195.     global gColorAvail := false;
  196.     global gStatus := '';
  197.     global gPartner;
  198.  
  199. # Set the actor's behavior.
  200.     MouseSpeed( 18 );
  201.     Patience( 4 );
  202.  
  203. # Open up communications session with the other target.
  204.     println "Attempting to 'open' communication with target {gPartner}.";
  205.     while not (gStatus = 'open') 
  206.     begin
  207.         gStatus := OpenSession([actor t:gPartner]);
  208.     end;
  209.  
  210. # Get information about the target's environment.
  211.     match [ screen r:?gScreenDim m:true ];
  212.     match [ menu t:?theFifthMenu o:5 ];
  213.  
  214.     if ( theFifthMenu = 'Colors' ) 
  215.         gColorAvail := true;
  216.     else 
  217.         gColorAvail := false;
  218.  
  219. # Create a new file and set up the window to the appropriate size.
  220.     println "Creating and setting up new DrawShapes document.";
  221.     select [ menuItem t:'New' m:[ menu t:'File' ]];
  222.     drag [ window t:/Untitled-≈/ o:1 ] a:{ 1, 21 };
  223.     size [ window t:/Untitled-≈/ o:1 ] w:(gScreenDim[3] - 2) h:(gScreenDim[4] - 22);
  224.     match [ window r:?gWindowDim o:1 ];
  225.     if ( Diff( (gWindowDim[2] + gWindowMod[2]), 
  226.             (gWindowDim[4] + gWindowMod[4]) ) > 645 ) 
  227.     begin
  228.         temp := WindowDim[2] + gWindowMod[2] + 645 - gWindowDim[4];
  229.         gWindowMod := Replace(temp,4,gWindowMod);
  230.     end;
  231.  
  232. # Drawshapes and communicate with the other target.
  233.     currentIteration := 1;
  234.     while ( currentIteration <= maxIterations )  
  235.     begin
  236.         println "In iteration number {currentIteration}.";
  237.         send([actor t:gPartner],{'go'});
  238.         println "Go {gPartner}!";
  239.         HoldForOther();
  240.         SelectTool( 1, true );
  241.         DrawRandomShapeWithTool( );
  242.         SelectShade( 1, true );
  243.         SelectColor( 1, true );
  244.         currentIteration := currentIteration + 1;
  245.     end;
  246.  
  247. # Save the file and end the communications session.
  248.     println "Saving document and closing session.";
  249.     DoSaveAs( saveAsFilename );
  250.     CloseSession([actor t:gPartner]);
  251.     println "Finished!";
  252. end; 
  253. # End main body *****************************************************
  254.